package com.panda.web.service.aims.impl;

import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panda.web.domain.dto.QuestionsDTO;
import com.panda.web.domain.vo.QuestionsVO;
import com.panda.web.entity.aims.Questions;
import com.panda.web.mapper.aims.QuestionsMapper;
import com.panda.web.service.aims.IQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;

import lombok.extern.slf4j.Slf4j;
import com.panda.web.utils.pageutils.PageResult;
import org.springframework.transaction.annotation.Transactional;


/**
 * 面试题库Service业务层处理
 *
 * @author cpanda
 * @date 2025-05-21
 */
@Slf4j
@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions> implements IQuestionsService {
    @Autowired
    QuestionsMapper questionsMapper;


    /**
     * 分页查询
     *
     * @param dto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<QuestionsVO> PageList(QuestionsDTO dto, Integer pageNum, Integer pageSize) {

        Page<Questions> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Questions> queryWrapper = new QueryWrapper();//需要处理查询条件
        IPage<Questions> questionsIPage = questionsMapper.selectPage(page, queryWrapper);
        long total = questionsIPage.getTotal();
        List<Questions> records = questionsIPage.getRecords();
        List<QuestionsVO> voList = questionsVoToList(records);//封装vo数据方法

        return new PageResult<>(voList, total, pageNum, pageSize);
    }

    /**
     * 无分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public List<QuestionsVO> choiceList(QuestionsDTO dto) {
        QueryWrapper<Questions> queryWrapper = new QueryWrapper();//需要处理查询条件
        queryWrapper.orderByDesc("creation_time");
        if (Objects.equals(dto.getQuestion(), null)) {
            queryWrapper.like("question", dto.getQuestion());
        }
        if (Objects.equals(dto.getType(), null)) {
            queryWrapper.like("type", dto.getType());
        }
        //构建查询条件
        List<Questions> questionsList = questionsMapper.selectList(queryWrapper);
        List<QuestionsVO> voList = questionsVoToList(questionsList);//封装vo数据方法
        return voList;
    }

    /**
     * 回显数据
     *
     * @param questionsId
     * @return
     */
    @Override
    public QuestionsVO selectById(Long questionsId) {
        QuestionsVO vo = new QuestionsVO();
        Questions questions = questionsMapper.selectById(questionsId);
        BeanUtils.copyProperties(questions, vo);
        return vo;
    }

    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @Override
    public int insert(QuestionsDTO dto) {
        Questions questions = new Questions();
        BeanUtils.copyProperties(dto, questions);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        questions.setCreationTime(format);
        questionsMapper.insert(questions);
        return 1;
    }

    /**
     * 修改数据
     *
     * @param dto
     * @return
     */
    @Override
    public int updateQuestionsById(QuestionsDTO dto) {
        Questions questions = new Questions();
        BeanUtils.copyProperties(dto, questions);
        questionsMapper.updateById(questions);
        return 1;
    }

    /**
     * 删除数据(逻辑删除)
     * @param ids
     * @return
     */
    @Override
    public int removeByQuestionsIds(List<Long> ids) {
        for (Long id : ids) {
            questionsMapper.deleteById(id);
        }
        return 1;
    }


    // 处理VO数据封装
    private List<QuestionsVO> questionsVoToList(List<Questions> questionsList) {
        List<QuestionsVO> voList = new ArrayList<>();
        if (questionsList.size() > 0 && !questionsList.isEmpty()) {
            for (Questions questions : questionsList) {
                QuestionsVO vo = new QuestionsVO();
                BeanUtils.copyProperties(questions, vo);
                voList.add(vo);
            }
        }
        return voList;
    }

//    /**
//     * 返回选中题目
//     * @param dto
//     * @return
//     */
//    @Transactional
//    @Override
//    public synchronized int getEnterprise(QuestionsDTO dto) throws NoApiKeyException, InputRequiredException {
//        //判断用户选择的是央企还是国企
//        String openid = dto.getOpenid();//用户id
//        List<Long> historyIds = historyMapper.findByOpenid(openid);//该用户的题目历史
//        Integer count = questionsMapper.findByType(dto.getType());
//        if (count==historyIds.size()){//已经抽完了 删除
//            historyMapper.deleteByOpenid(openid);
//        }
//        QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("type",dto.getType());
//        List<Questions> questionsList = questionsMapper.selectList(queryWrapper);
//        Questions questions = questionsList.get(0);
//        //判断用户已选择过得的题目
//        //调用api 传给大模型
//        ApplicationParam param = ApplicationParam.builder()
//                // 若没有配置环境变量，可用百炼API Key将下行替换为：.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中，以减少API Key泄露风险。
//                .apiKey("sk-2f3cd9a0de5547009da502d70b46c770")
//                .appId("197ac8f0267a42ae99eb8390107e054a")
//                .prompt(questions.getQuestion()+questions.getAnswer())
//                .build();
//
//        Application application = new Application();
//        ApplicationResult result = application.call(param);
//
//        System.out.printf("text: %s\n",
//                result.getOutput().getText());
//        return 1;
//    }



}




















